/*
Copyright 2010,2011 Matt Van Der Westhuizen

This file is part of CGLL.

CGLL is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

CGLL is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with CGLL.  If not, see <http://www.gnu.org/licenses/>.
*/

package org.chaoticengine.cgll.ui;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.geom.Rectangle;
import org.newdawn.slick.geom.Vector2f;
import org.newdawn.slick.state.StateBasedGame;

/**
 * A simple UI widget that displays an icon, which can optionally have it's
 * transparency varied in updates to make it flicker or indicate charge levels.
 *
 * @author Matt v.d. Westhuizen
 */
public abstract class IconWidget extends Widget {
    protected Image icon = null;
    protected float alpha = 1.0f;

    public IconWidget(Image icon) {
        this.icon = icon;
        this.alpha = 1.0f;
    }

    public float getAlpha() {
        return(this.alpha);
    }

    public void setAlpha(float newAlpha) {
        if (newAlpha < 0.0f) {
            alpha = 0.0f;
        } else if (newAlpha > 1.0f) {
            alpha = 1.0f;
        } else {
            alpha = newAlpha;
        }
    }

    @Override
    public Rectangle getBounds() {
        return(new Rectangle(0.0f, 0.0f, icon.getWidth(), icon.getHeight()));
    }

    @Override
    public void render(Vector2f position, GameContainer gc, StateBasedGame sb, Graphics gr) {
        icon.setAlpha(alpha);
        gr.drawImage(icon, position.x, position.y);
    }

    public Image getImage() {
        return(icon);
    }
}
